package com.jswzc.common.api.service.smsUnified;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jswzc.api.service.smsUnified.SmsUnifiedServiceI;
import com.jswzc.dao.mdao.DaoSupport;
import com.jswzc.framework.util.Tools;
import com.jswzc.framework.util.smsUtil.qqSms.SmsUtilQQ;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by DeathCat on 2017/05/12.
 */
@Service("smsUnifiedService")
public class SmsUnifiedServiceImpl implements SmsUnifiedServiceI {

    @Resource(name = "daoSupport")
    private DaoSupport dao;


    @Override
    public Map<String, Object> sendSms(String mobile,String content,String smsCode,String smsAuthorCode) throws Exception {
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("res", "F");
        res.put("msg", "未知错误");
        boolean b = true;
        //1.通过smsCode 加载发送短信的具体参数
        Map<String,Object> param = new HashMap<>();
        param.put("smsCode",smsCode);
        param.put("smsAuthorCode",smsAuthorCode);
        Map map=(HashMap)dao.findForObject("smsDao.findSmsParam",param);
        Map<String, Object> qqres = new HashMap<String, Object>();
        if (map!=null){
            switch (smsCode){
                //腾讯短信发送平台
                case "SMS-1001":
                    qqres = sendSms1001(mobile, content, map);
                    break;
                case "SMS-1002":
                    //填充短信模板数据
                    if(map.containsKey("smsTemplate")){
                        content=replaceSmsTemplate(content,map.get("smsTemplate").toString());
                    }
                    qqres = sendSms1001(mobile, content, map);
                    break;
                case "SMS-1003":
                    //填充短信模板数据 五峰短信
                    if(map.containsKey("smsTemplate")){
                        content=replaceSmsTemplate(content,map.get("smsTemplate").toString());
                    }
                    qqres = sendSms1001(mobile, content, map);
                    break;
                case "SMS-1004":
                    //填充短信模板数据 五峰预约
                    if(map.containsKey("smsTemplate")){
                        content=map.get("smsTemplate").toString();
                    }
                    qqres = sendSms1001(mobile, content, map);
                    break;
                default:
                    res.put("msg", "短信接口类型：type不正确，不是目前支持的类型");
                    break;
            }
        }
        return qqres;
    }

    /**
     * 替换模板
     * @param content 原来参数
     * @param smsTemplate 模板结构
     * @return
     */
    private String replaceSmsTemplate(String content,String smsTemplate){
        if(!Tools.isEmpty(smsTemplate)){
            content=smsTemplate.replace("{data}", content);
        }
        return content;
    }

    /**
     * 发送短信-腾讯
     * @param mobile
     * @param content
     * @param param
     * @return
     */
    private Map<String, Object> sendSms1001(String mobile, String content, Map param) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("res", "F");
        map.put("msg", "未知问题");
        JSONObject jsonParm = null;
        try {
            jsonParm = JSON.parseObject(param.get("smsParms").toString());
        } catch (Exception e) {
            map.put("msg", "接口参数：获取短信配置异常" + e.getMessage());
            return map;
        }
        if (jsonParm != null) {
            String appid = null;
            String appkey = null;
            String nationCode = null;
            try {
                appid = jsonParm.get("appid").toString();
            } catch (Exception e) {
                map.put("msg", "接口参数：appid字段异常");
                return map;
            }
            try {
                appkey = jsonParm.get("appkey").toString();
            } catch (Exception e) {
                map.put("msg", "接口参数：appkey字段不可为空");
                return map;
            }
            try {
                nationCode = jsonParm.get("nationCode").toString();
            } catch (Exception e) {
                map.put("msg", "接口参数：nationCode字段不可为空");
                return map;
            }

            SmsUtilQQ sender = new SmsUtilQQ(Integer.parseInt(appid + ""), appkey);
            String json = sender.sendMsg(nationCode, mobile.trim(), content);
            //根据短信接口返回值判断成功与否
            JSONObject jsonobj = JSON.parseObject(json);
            String result = jsonobj.get("result").toString();
            if (!"0".equals(result)) {
                String msg = jsonobj.get("errmsg").toString();
                map.put("msg", msg);
            } else {
                map.put("res", "T");
                map.put("msg", "操作成功");
            }
        }
        return map;
    }


    @Override
    public Map<String, Object> sendSmsForQQ(String mobile, String content, String appid, String appkey, String nationCode) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("res", "F");
        map.put("msg", "未知问题");
        SmsUtilQQ sender = new SmsUtilQQ(Integer.parseInt(appid + ""), appkey);
        String json = sender.sendMsg(nationCode, mobile.trim(), content);
        try {
            //根据短信接口返回值判断成功与否
            JSONObject jsonobj = JSON.parseObject(json);
            String result = jsonobj.get("result").toString();
            if (!result.equals("0")) {
                String msg = jsonobj.get("errmsg").toString();
                map.put("msg", msg);
            } else {
                map.put("res", "T");
                map.put("msg", "操作成功");
            }
        } catch (Exception e) {
            map.put("msg", "未知问题：" + e.getMessage());
        }
        return map;
    }


    @Override
    public Map<String, Object> sendSmsForRW(String mobile, String content) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        //待开发

        return map;
    }


}
